﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

namespace APISDKDemo.Common
{
    public class SDKClient
    {
        private string appKey;
        private string appSecret;
        private string serverRoot;//http://127.0.0.1:8888/api/v1/
        public SDKClient(string appKey, string appSecret, string serverRoot)
        {
            this.appKey = appKey;
            this.appSecret = appSecret;
            this.serverRoot = serverRoot;
        }

        /// <summary>
        /// Get请求
        /// </summary>
        /// <param name="url">要请求的地址</param>
        /// <param name="queryStringData">querystring参数键值对</param>
        /// <returns></returns>
        public async Task<SDKResult> GetAsync(string url, IDictionary<string, string> queryStringData)
        {
            if (queryStringData == null)
            {
                throw new ArgumentNullException("queryStringData不能为null");
            }

            var qsItems = queryStringData.OrderBy(kv => kv.Key).Select(kv => kv.Key + "=" + kv.Value);

            string queryString = string.Join("&", qsItems);

            string sign = EncryptExtension.MD5Hash(queryString + appSecret);

            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Add("appKey", appKey);
                httpClient.DefaultRequestHeaders.Add("sign", sign);
                var resp = await httpClient.GetAsync(serverRoot + url + "?" + queryString);
                SDKResult sdkResult = new SDKResult();
                sdkResult.Result = await resp.Content.ReadAsStringAsync();
                sdkResult.StatusCode = resp.StatusCode;

                return sdkResult;
            }
        }

        /// <summary>
        /// Post请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="queryStringData"></param>
        /// <returns></returns>
        public async Task<SDKResult> PostAsync(string url, IDictionary<string, string> formStringData)
        {
            if (formStringData == null)
            {
                throw new ArgumentNullException("formStringData不能为null");
            }

            var qsItems = formStringData.OrderBy(kv => kv.Key).Select(kv => kv.Key + "=" + kv.Value);

            string formString = string.Join("&", qsItems);

            string sign = EncryptExtension.MD5Hash(formString + appSecret);

            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Add("appKey", appKey);
                httpClient.DefaultRequestHeaders.Add("sign", sign);

                FormUrlEncodedContent content = new FormUrlEncodedContent(formStringData);

                var resp = await httpClient.PostAsync(serverRoot + url, content);

                SDKResult sdkResult = new SDKResult();
                sdkResult.Result = await resp.Content.ReadAsStringAsync();
                sdkResult.StatusCode = resp.StatusCode;

                return sdkResult;
            }

        }
    }

    public class SDKResult
    {
        public string Result { get; set; }
        public HttpStatusCode StatusCode { get; set; }
    }
}
